package com.andy.controller;

import com.andy.customerService.vo.JsonBaseResult;
import com.andy.entity.FlowInfo;

import com.andy.entity.HistoricActivityInstanceEntity;

import com.andy.entity.UserTaskRequest;
import com.andy.service.impl.ActFlowCommServiceImpl;
import com.andy.service.impl.FlowServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 流程管理
 */

@RestController
@RequestMapping("/flow")
@Slf4j
public class FlowController {


    @Autowired
    private FlowServiceImpl flowService;
    @Autowired
    private ActFlowCommServiceImpl actFlowCommService;

    /**
     * 开启流程实例
     * @author: Andy
     * @dateTime: 2022/1/19 12:18
     */
    @GetMapping("/start")
    public JsonBaseResult<Object> startProcessAndy(@RequestParam String businessKey) {
        return actFlowCommService.startProcessAndy(businessKey);
    }

    /**
     * 根据userId查询用户任务
     * @params: [userId]
     * @author: Andy
     * @dateTime: 2022/1/19 13:26
     */
    @GetMapping("/findUserTask")
    public List<Map<String, Object>> findUserTask(@RequestParam String userId,@RequestParam(required = false) String businessKey) {
//        Long userId = (Long)request.getSession().getAttribute("userid");
        List<Map<String, Object>> userTask = flowService.findUserTask(userId,businessKey);
        return userTask;
    }

    /**
     * 执行任务
     * @params: [taskId, userId]
     * @author: Andy
     * @dateTime: 2022/1/19 13:37
     */
    @PostMapping("/completeTask")
    public JsonBaseResult<Object> completeTask(@RequestBody UserTaskRequest userTaskRequest) {
        return flowService.completeTask(userTaskRequest);
    }

    /**
     * 根据 businessKey查询历史记录
     * @author: Andy
     * @dateTime: 2022/1/19 12:22
     */
    @GetMapping("/findFlowTask")
    public List<HistoricActivityInstanceEntity> findFlowTask(@RequestParam String businessKey) {
//        String businessKey = "evection:"+id;
//        String businessKey = "tomKey";
        return actFlowCommService.searchHistory(businessKey);

    }

    /**
     * 查询所有流程
     *
     * @return
     */
    @GetMapping("/findAll")
    public List<FlowInfo> findAllFlow() {
        return flowService.findAllFlow();
    }

    /**
     * 部署流程
     *
     * @param request
     * @return 0-部署失败  1- 部署成功  2- 已经部署过
     */
    @PutMapping("/flow/deployment/{id}")
    public Integer deployment(HttpServletRequest request, @PathVariable(name = "id") Long id) {
        FlowInfo flowInfo = flowService.findOneFlow(id);
        if (flowInfo.getState() == 0) {
            return 2;
        }
        actFlowCommService.saveNewDeploy(flowInfo);
        return flowService.updateDeployState(id);
    }


    /**
     * 查询用户任务
     *
     * @return
     */
//    @GetMapping("/flow/findUserTask")
//    public List<Map<String,Object>> findUserTask(HttpServletRequest request){
//        Long userId = (Long)request.getSession().getAttribute("userid");
//        return flowService.findUserTask(userId);
//    }



    /**
     * 查询任务详细信息
     * @param request
     * @return
     */
    @GetMapping("/flow/findTaskInfo")
    public List<Map<String,Object>> findTaskInfo(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userid");
        return flowService.findTaskInfo(userId);
    }




  




 

 

    /**
     * 查看流程定义内容--暂不可用
     * 前提是activiti7 可以自动部署流程 只需将BPMN文件放在 /resources/process 文件夹下
     * @author: Andy
     * @dateTime: 2022/1/19 12:12
     */
    @GetMapping("/all")
    public void findAllProcess() {
        actFlowCommService.findAllProcess();
    }









}
